﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_MessageHandlerInspector
	{
		private class MyMessage : Message
		{
		}
		private class MyInformation : Information
		{
		}
		private class MyOperation : Operation<String, Int32>
		{
		}

		private abstract class TestBaseClass
		{
			public abstract void Method2(MyMessage message);

			[MessageHandler]
			public abstract void Method3(MyMessage message);
		}

		private class TestClass : TestBaseClass
		{
			public void Method1(MyMessage message)
			{
				// no attribute
			}

			[MessageHandler]
			public override void Method2(MyMessage message)
			{
				// abstract method with attribute
			}

			public override void Method3(MyMessage message)
			{
				// abstract method with attribute derived
			}

			[MessageHandler]
			public void Method4<T>(MyMessage message)
			{
				// generic method
			}

			[MessageHandler]
			public void Method5()
			{
				// no parameters
			}

			[MessageHandler]
			public void Method6(MyMessage message, Object dummy)
			{
				// too many parameters
			}

			[MessageHandler]
			public void Method7(Object message)
			{
				// invalid parameter type
			}

			[MessageHandler]
			public String Method8(MyMessage message)
			{
				// invalid return type
				return null;
			}

			[MessageHandler]
			public void Method9(Information message)
			{
				// information message
			}

			[MessageHandler]
			public void Method10(MyOperation message)
			{
				// operation message
				message.Complete("done");
			}

			[MessageHandler]
			public void Method11(Operation message)
			{
				// operation message
				message.Cancel();
			}

			[MessageHandler]
			public async void Method12(MyMessage message)
			{
				// invalid return type
				await Task.Factory.StartNew(() => Thread.Sleep(100));
			}

			[MessageHandler]
			public async Task Method13(MyMessage message)
			{
				await Task.Factory.StartNew(() => Thread.Sleep(100));
			}

			[MessageHandler]
			public Task Method14(MyMessage message)
			{
				// non-async method with Task return type
				return null;
			}
		}


		private MethodInfo GetMethodInfo(String name)
		{
			var target = new TestClass();
			return target.GetType().GetMethod(name);
		}

		private HandlerInfo InspectMethod(String name)
		{
			var methodInfo = GetMethodInfo(name);
			var inspector = new MessageHandlerInspector();
			return inspector.Inspect(methodInfo);
		}


		// --- TESTS ---


		[Test]
		public void Test_Inspect_NoAttribute()
		{
			var handlerInfo = InspectMethod("Method1");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_AbstractMethod_DerivedMethodWithAttribute()
		{
			var handlerInfo = InspectMethod("Method2");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handlerInfo.IsAsync, Is.False);
		}

		[Test]
		public void Test_Inspect_AbstractMethod_BaseMethodWithAttribute()
		{
			var handlerInfo = InspectMethod("Method3");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handlerInfo.IsAsync, Is.False);
		}

		[Test]
		public void Test_Inspect_GenericMethod()
		{
			var handlerInfo = InspectMethod("Method4");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_NoParameter()
		{
			var handlerInfo = InspectMethod("Method5");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_TwoParameters()
		{
			var handlerInfo = InspectMethod("Method6");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_ParameterNotMessageType()
		{
			var handlerInfo = InspectMethod("Method7");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_ReturnTypeNotVoid()
		{
			var handlerInfo = InspectMethod("Method8");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_InformationMessage()
		{
			var handlerInfo = InspectMethod("Method9");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(Information)));
			Assert.That(handlerInfo.IsAsync, Is.False);
		}

		[Test]
		public void Test_Inspect_OperationMessage()
		{
			var handlerInfo = InspectMethod("Method10");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyOperation)));
			Assert.That(handlerInfo.IsAsync, Is.False);
		}

		[Test]
		public void Test_Inspect_OperationMessage_BaseType()
		{
			var handlerInfo = InspectMethod("Method11");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(Operation)));
			Assert.That(handlerInfo.IsAsync, Is.False);
		}

		[Test]
		public void Test_Inspect_AsyncMethod_Void_ReturnType()
		{
			var handlerInfo = InspectMethod("Method12");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_AsyncMethod_Task_ReturnType()
		{
			var handlerInfo = InspectMethod("Method13");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handlerInfo.IsAsync, Is.True);
		}

		[Test]
		public void Test_Inspect_Method_Task_ReturnType()
		{
			var handlerInfo = InspectMethod("Method14");

			Assert.That(handlerInfo, Is.Null);
		}

	}
}

#endif
